package plogg;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * This gets instantiated and passed around. This models an ideal plogg where it holds all the data 
 * (i.e. we don't need to worry about where the data is coming from). Also note that although we
 * use the bluetoothID for convenience, just refer to it as the unique ID in general.
 */
public class Plogg {
	/**
	 * Returns an array of available plogg devices, including registered ones 
	 * which aren't in range.
	 * @return ArrayList containing available ploggs
	 */
	public static ArrayList<Plogg> getAvailablePloggs()	{
		// Get a local *static* reference to bluetooth as opposed to the
		// normal instance one
		BluetoothDevice bluetooth = BluetoothDevice.getInstance();
		
		// Bluetooth scan for IDs
		HashSet<String> btEntries = bluetooth.getAllDevices();
		
		// Database scan
		HashSet<String> dbEntries = Database.getAllDevices();
		
		// Combine the two in a set with no duplicates
		dbEntries.addAll(btEntries);
		
		// TODO: update the database appropriately?
		
		// Make an ArrayList of Ploggs
		ArrayList<Plogg> returnSet = new ArrayList<Plogg>();
		for(String nextID : dbEntries){
			returnSet.add(new Plogg(nextID));
		}
		
		// Return the set
		return returnSet;
	}
	
	/*
	 * Instantiated stuff: ---------------------------------------------------------------------------
	 */
	
	BluetoothDevice bluetooth;
	
	// Plogg initialisation section ------------------------------------------------------------------	
	private String 	ploggID		= "0000000000", 
					ploggName	= "NoName";
	
	/**
	 * Get the plogg info from the device and the database. Device updates database? Or other 
	 * way around?
	 * @param fromID	String containing the ploggID
	 */
	public Plogg(String fromID){
		bluetooth = BluetoothDevice.getInstance();
		
		ploggID = fromID; 
		
		bluetooth.setPloggDefaults(ploggID, ploggName);
	}
	
	
	/**
	 * toString
	 */
	public String toString()	{
		return ploggName + " (" + ploggID + ")";
	}
	
	// Getter methods -----------------------------------------------------------------------------
	
	
	/**
	 * gets the ID of the plogg
	 * @return ploggID
	 */
	public String getID()	{
		return ploggID;
	}
	
	
	/**
	 * gets the name of the plogg
	 * @return plogg Name
	 */
	public String getName()	{
		return ploggName;
	}
}
